package com.glsc.ngateway.platform.repo.costallocation;

import com.glsc.ngateway.platform.domain.costallocation.IpInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Repository
public interface IpInfoRepo extends JpaRepository<IpInfo, Long>, JpaSpecificationExecutor<IpInfo> {
    @Query(value = "select t from IpInfo t where t.id in :ids")
    List<IpInfo> queryByIDS(@Param("ids") Set<Long> ids);
    @Query(value = "select t from IpInfo t where t.systemUserId like  CONCAT('%',:userOaAccount,'%')")
    List<IpInfo> queryByUserOaAccount(@Param("userOaAccount") String userOaAccount);
    @Query(value = "select t from IpInfo t where t.ip = :ip")
    IpInfo queryByIP(@Param("ip") String ip);

    @Transactional
    @Modifying(clearAutomatically = true,flushAutomatically = true)
    @Query(value = "update t_ip_info u set u.vc_source_type = :sourceType ,u.vc_virtual_name = :virtualName ,u.vc_ip = :ip," +
            "u.vc_ip_status = :ipStatus,u.vc_system_user_id =:systemUserId ,u.vc_operator = :operator" +
            ",u.vc_system_user_name =:systemUserName ,u.vc_has_writed = :hasWrited," +
            "u.dt_update_time =:updatetime where u.l_id = :id",nativeQuery = true)
    void updateIpInfoId(@Param("id") Long id, @Param("sourceType") String sourceType,
                            @Param("ip") String ip,@Param("updatetime") LocalDateTime updatetime,
                            @Param("systemUserName") String systemUserName,
                            @Param("ipStatus") String ipStatus,@Param("systemUserId") String systemUserId,
                            @Param("operator") String operator,@Param("virtualName") String virtualName,
                        @Param("hasWrited") String hasWrited);


    @Transactional
    @Modifying
    @Query(value = "delete from IpInfo f where f.uniqueKey = :uniqueKey")
    void deleteByUniquekey(@Param("uniqueKey") String uniqueKey);
    @Query(value = "select t from IpInfo t where t.uniqueKey = :uniqueKey")
    IpInfo queryByUniquekey(@Param("uniqueKey") String uniqueKey);

    @Query(value = "select t from IpInfo t where t.uniqueKey in :uniqueKeySet")
    List<IpInfo> queryByUniquekeySet(@Param("uniqueKeySet") Set uniqueKeySet);

}
